import {useHwSettingAuthControl} from '@@/hw-auth/components/SettingAuthContext';
import {useHwAuthControl} from '@@/hw-auth/components/AuthContext';
import { singleAuth } from '@@/hw-auth/utils/check';
import {AuthObjType,CheckerFn,CheckAuthParams} from '@@/hw-auth/modal.d'

const getChecker=(checker?:CheckerFn)=>{
    if (checker){
      return checker;
    }
    const {settingAuthChecker}=useHwSettingAuthControl();
    if (settingAuthChecker){
      return  settingAuthChecker;
    }
    const {pageAuthChecker}=useHwAuthControl();
    return pageAuthChecker;
}//获取验证器
export function useCheckAuth({auth,...config}:CheckAuthParams){
    const {checker,...otherConfig}=config;
    const {resultType="bool"}=otherConfig;
    const relChecker=getChecker(checker);
    const isArray=Array.isArray(auth);
    if (!isArray){
     return  singleAuth(auth,{...otherConfig,checker:relChecker});
    }
    const arrayAuth=(auth as (AuthObjType|string)[]);
    const resultAuth:any[]=[];
    arrayAuth.forEach((item)=>{
        const result=singleAuth(item,{...otherConfig,checker:relChecker});
        if(!result){
            return;
        }
        if (resultType==="bool"){
            resultAuth.push(item);
            return;
        }
        resultAuth.push(result);
    });
    return resultAuth;
}
